package com.shuwang.jh_shop.ui.base

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.Toast
import com.shuwang.jh_shop.R


/**
 * 作者  Administrator
 * 时间  2016/10/10 14:14
 * 描述  ViewPager在加载Fragment的时候会预加载  延迟加载Fragment
 */

open class LazyFragment : BaseLazyFragment() {
    private var isInit = false
    private var savedInstanceState: Bundle? = null
    protected var isLazyLoad = true//默认延迟加载
    private var layout: FrameLayout? = null
    private var isStart = false
    protected var bundle: Bundle? = null//这是我传过来的值

    /**
     * @param savedInstanceState
     */
    override fun onCreateView(savedInstanceState: Bundle?) {
        bundle = arguments
        if (bundle != null) {
            isLazyLoad = bundle!!.getBoolean(INTENT_BOOLEAN_LAZYLOAD, isLazyLoad)
        }
        if (isLazyLoad) {
            if (userVisibleHint && !isInit) {
                this.savedInstanceState = savedInstanceState
                onCreateViewLazy(savedInstanceState)
                isInit = true
            } else {
                layout = FrameLayout(applicationContext!!)
                layout!!.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
                val view = LayoutInflater.from(applicationContext).inflate(R.layout.fragment_lazy_loading, null)
                layout!!.addView(view)
                super.setContentView(layout!!)
            }
        } else {
            onCreateViewLazy(savedInstanceState)
            isInit = true
        }
    }

    /**
     * @param isVisibleToUser 如果Fragment可见该方法就会被调用
     */
    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser && !isInit && getContentView()!= null) {
            onCreateViewLazy(savedInstanceState)
            isInit = true
            onResumeLazy()
        }
        if (isInit && getContentView() != null) {
            if (isVisibleToUser) {
                isStart = true
                onFragmentStartLazy()
            } else {
                isStart = false
                onFragmentStopLazy()
            }
        }
    }

    override fun setContentView(layoutResID: Int) {
        if (isLazyLoad && getContentView() != null && getContentView()?.parent != null) {
            layout!!.removeAllViews()
            val view = inflater!!.inflate(layoutResID, layout, false)
            layout!!.addView(view)
        } else {
            super.setContentView(layoutResID)
        }
    }

    override fun setContentView(view: View) {
        if (isLazyLoad && getContentView() != null && getContentView()?.parent != null) {
            layout!!.removeAllViews()
            layout!!.addView(view)
        } else {
            super.setContentView(view)
        }
    }

    @Deprecated("")
    override fun onStart() {
        super.onStart()
        if (isInit && !isStart && userVisibleHint) {
            isStart = true
            onFragmentStartLazy()
        }
    }

    @Deprecated("")
    override fun onStop() {
        super.onStop()
        if (isInit && isStart && userVisibleHint) {
            isStart = false
            onFragmentStopLazy()
        }
    }

    //当Fragment被滑到可见的位置时，调用
    protected fun onFragmentStartLazy() {
        Log.d(TAG, "onFragmentStartLazy() called with: " + "")
    }

    //当Fragment被滑到不可见的位置，offScreen时，调用
    protected fun onFragmentStopLazy() {
        Log.d(TAG, "onFragmentStopLazy() called with: " + "")
    }

    //每次切换好像都会调用改方法
    protected fun onCreateViewLazy(savedInstanceState: Bundle?) {
        Log.d(TAG, "onCreateViewLazy() called with: savedInstanceState = [$savedInstanceState]")
    }

    protected fun onResumeLazy() {
        Log.d(TAG, "onResumeLazy() called with: " + "")
    }

    protected fun onPauseLazy() {
        Log.d(TAG, "onPauseLazy() called with: " + "")
    }

    protected fun onDestroyViewLazy() {

    }

    @Deprecated("")
    override fun onResume() {
        Log.d(TAG, "onResume() : " + "getUserVisibleHint():" + userVisibleHint)
        super.onResume()
        if (isInit) {
            onResumeLazy()
        }
    }

    @Deprecated("")
    override fun onPause() {
        Log.d(TAG, "onPause() : " + "getUserVisibleHint():" + userVisibleHint)
        super.onPause()
        if (isInit) {
            onPauseLazy()
        }
    }

    /**
     * 如果页面被销毁，onCreateView会被从新调用
     * Init也需要被默认初始化啊
     */
    @Deprecated("")
    override fun onDestroyView() {
        Log.d(TAG, "onDestroyView() : " + "getUserVisibleHint():" + userVisibleHint)
        super.onDestroyView()
        if (isInit) {
            onDestroyViewLazy()
        }
        isInit = false
    }

    fun showToast(msg: String) {
        Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show()
    }

    companion object {
        private val TAG = "LazyFragment"
        val INTENT_BOOLEAN_LAZYLOAD = "intent_boolean_lazyLoad"
    }

    open fun onCreateViewLazy() {}
}
